########################################################################################################################################################
# Script: New refugees' integration processes and their future mobility intentions                                                                     #
# �zge Bilgili, Meta van der Linden & Floris Peters                                                                                                    #
# Published in A. Smeekes & J. Thijs (Eds.), Migration and Ethnic Relations: Current directions for theory and research (pp. 13-28). Proefschriftmaken #
########################################################################################################################################################


################
# Introduction #
################

# This file provides the syntax used to create all the figures in the paper. 

#######################################################################################################################
#######################################################################################################################


############     
# Figure 1 #
############  

rm(list=ls(all=TRUE))
library(ggplot2)
library(RColorBrewer)
library(MASS)
library(dotwhisker)
library(broom)
library(grid)
library(gridExtra)

term1.1 <- rbind(2015,2016,2017,2018,2019,2020,2021,
                 2015,2016,2017,2018,2019,2020,2021,
                 2015,2016,2017,2018,2019,2020,2021)

estimate1.1 <- rbind(43095,19370,16145,20510,22540,13720,24740,
                     18675,2865,2970,2960,3675,4070,8380,
                     7360,1860,1590,1410,500,370,770)

group_var1.1 <- rbind("Total","Total","Total","Total","Total","Total","Total",
                      "Syria","Syria","Syria","Syria","Syria","Syria","Syria",
                      "Eritrea","Eritrea","Eritrea","Eritrea","Eritrea","Eritrea","Eritrea")
group_var1.1 <- factor(group_var1.1, levels = c("Total", "Syria", "Eritrea"))

dataset1.1 <- data.frame(term1.1,estimate1.1,group_var1.1)

plot1.1 <- ggplot(data=dataset1.1, aes(x=term1.1, y=estimate1.1, linetype = group_var1.1)) +
  geom_line(size = 0.8) +
  geom_point(shape = group_var1, size = 4) +
  theme(legend.title = element_blank(),
        legend.text=element_text(size=12),
        axis.text=element_text(size=12),
        legend.position = "bottom",
        plot.title = element_text(size = 20)) +
  scale_linetype_manual(values=c("solid", "twodash", "dotted")) +
  scale_x_continuous(breaks=c(2015,2016,2017,2018,2019,2020,2021)) +
  xlab("") + ylab("") +
  labs(title = "(A)")

term1.2 <- rbind(2015,2016,2017,2018,2019,2020,2021,
                 2015,2016,2017,2018,2019,2020,2021,
                 2015,2016,2017,2018,2019,2020,2021,
                 2015,2016,2017,2018,2019,2020,2021)

estimate1.2 <- rbind(2550,1025,320,325,435,390,3005,
                     1885,885,720,1870,1535,370,265,
                     3010,960,845,745,620,335,745,
                     55,235,480,1300,1250,990,2460)

group_var1.2 <- rbind("Afghanistan","Afghanistan","Afghanistan","Afghanistan","Afghanistan","Afghanistan","Afghanistan",
                      "Iran","Iran","Iran","Iran","Iran","Iran","Iran",
                      "Iraq","Iraq","Iraq","Iraq","Iraq","Iraq","Iraq",
                      "Turkey","Turkey","Turkey","Turkey","Turkey","Turkey","Turkey")

group_var1.2 <- factor(group_var1.2, levels = c("Afghanistan", "Iran", "Iraq", "Turkey"))

dataset1.2 <- data.frame(term1.2,estimate1.2,group_var1.2)

plot1.2 <- ggplot(data=dataset1.2, aes(x=term1.2, y=estimate1.2, linetype = group_var1.2)) +
  geom_line(size = 0.8) +
  geom_point(shape = group_var2, size = 4) +
  theme(legend.title = element_blank(),
        legend.text=element_text(size=12),
        axis.text=element_text(size=12),
        legend.position = "bottom",
        plot.title = element_text(size = 20)) +
  scale_linetype_manual(values=c("solid", "twodash", "dotted", "dotdash")) +
  scale_x_continuous(breaks=c(2015,2016,2017,2018,2019,2020,2021)) +
  ylim(0,3300) +
  xlab("") + ylab("") +
  labs(title = "(B)")

final_figure1 <- grid.arrange(plot1.1, plot1.2, ncol=2, nrow=1)

ggsave(filename = "Figure_1_asylum_applications", height = 6, width = 14, plot = final_figure1, device = 'pdf', path = 'C:/Users/3349977/Desktop/Liber Amicorum Verkuyten')


############     
# Figure 2 #
############  

rm(list=ls(all=TRUE))
library(ggplot2)
library(RColorBrewer)
library(MASS)
library(dotwhisker)
library(broom)
library(grid)
library(gridExtra)

term2 <- rbind("Syria","Eritrea","Afghanistan","Iraq","Iran","Turkey",
               "Syria","Eritrea","Afghanistan","Iraq","Iran","Turkey")

estimate2 <- rbind(0.423,0.794,0.640,0.540,0.523,0.120,0.577,0.206,0.360,0.460,0.477,0.880)

group_var2 <- rbind("Asylum","Asylum","Asylum","Asylum","Asylum","Asylum",
                    "Immigration","Immigration","Immigration","Immigration","Immigration","Immigration")

dataset2 <- data.frame(term2,estimate2,group_var2)

plot2 <- ggplot(data=dataset2, aes(fill = group_var2, x=term2, y=estimate2)) +
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_manual(values = c("black", "grey")) +
  xlab("") + ylab("") +
  theme(legend.title = element_blank(),
        legend.text=element_text(size=12),
        axis.text=element_text(size=9))

ggsave(filename = "Figure_2_immigration_prop", height = 6, width = 8, plot = plot2, device = 'pdf', path = 'C:/Users/3349977/Desktop/Liber Amicorum Verkuyten')


############     
# Figure 3 #
############ 

rm(list=ls(all=TRUE))
library(ggplot2)
library(RColorBrewer)
library(MASS)
library(dotwhisker)
library(broom)
library(grid)
library(gridExtra)

term3.1 <- rbind(2015,2016,2017,2018,2019,2020,
                 2015,2016,2017,2018,2019,2020)

estimate3.1 <- rbind(1226,1107,1164,940,968,689,
                     4520,3801,3704,3551,3347,3325)

group_var3.1 <- rbind("Iran","Iran","Iran","Iran","Iran","Iran",
                      "Turkey","Turkey","Turkey","Turkey","Turkey","Turkey")

group_var3.1 <- factor(group_var3.1, levels = c("Iran","Turkey"))

dataset3.1 <- data.frame(term3.1,estimate3.1,group_var3.1)

plot3.1 <- ggplot(data=dataset3.1, aes(x=term3.1, y=estimate3.1, linetype = group_var3.1)) +
  geom_line(size = 0.8) +
  geom_point(shape = group_var2, size = 4) +
  theme(legend.title = element_blank(),
        legend.text=element_text(size=12),
        axis.text=element_text(size=12),
        legend.position = "bottom",
        plot.title = element_text(size = 20)) +
  scale_linetype_manual(values=c("solid","dotted")) +
  scale_x_continuous(breaks=c(2015,2016,2017,2018,2019,2020,2021)) +
  xlab("") + ylab("") +
  ylim(0,6000) +
  labs(title = "(A)")


term3.2 <- rbind(2015,2016,2017,2018,2019,2020,
                 2015,2016,2017,2018,2019,2020,
                 2015,2016,2017,2018,2019,2020,
                 2015,2016,2017,2018,2019,2020)

estimate3.2 <- rbind(341,465,738,630,519,399,
                     45,64,94,104,108,77,
                     931,696,811,710,609,350,
                     698,703,839,853,742,723)

group_var3.2 <- rbind("Syria","Syria","Syria","Syria","Syria","Syria",
                      "Eritrea","Eritrea","Eritrea","Eritrea","Eritrea","Eritrea",
                      "Afghanistan","Afghanistan","Afghanistan","Afghanistan","Afghanistan","Afghanistan",
                      "Iraq","Iraq","Iraq","Iraq","Iraq","Iraq")

group_var3.2 <- factor(group_var3.2, levels = c("Syria","Eritrea","Afghanistan", "Iraq"))

dataset3.2 <- data.frame(term3.2,estimate3.2,group_var3.2)

plot3.2 <- ggplot(data=dataset3.2, aes(x=term3.2, y=estimate3.2, linetype = group_var3.2)) +
  geom_line(size = 0.8) +
  geom_point(shape = group_var3, size = 4) +
  theme(legend.title = element_blank(),
        legend.text=element_text(size=12),
        axis.text=element_text(size=12),
        legend.position = "bottom",
        plot.title = element_text(size = 20)) +
  scale_linetype_manual(values=c("dotted","longdash","solid", "twodash")) +
  scale_x_continuous(breaks=c(2015,2016,2017,2018,2019,2020,2021)) +
  xlab("") + ylab("") +
  ylim(0,1500) +
  labs(title = "(B)")

layout <- rbind(c(1,2))

final_figure3 <- grid.arrange(plot3.1, plot3.2, ncol=2, nrow=2, layout_matrix = layout)

ggsave(filename = "Figure_3_emigration", height = 7, width = 13, plot = final_figure3, device = 'pdf', path = 'C:/Users/3349977/Desktop/Liber Amicorum Verkuyten')


############     
# Figure 4 #
############  

rm(list=ls(all=TRUE))
library(ggplot2)
library(RColorBrewer)
library(MASS)
library(dotwhisker)
library(broom)
library(grid)
library(gridExtra)

term4.1 <- rbind(2015,2016,2017,2018,2019,2020,
                 2015,2016,2017,2018,2019,2020)

estimate4.1 <- rbind(171,69,61,114,689,7673,
                     48,36,24,31,100,2064)

group_var4.1 <- rbind("Syria","Syria","Syria","Syria","Syria","Syria",
                      "Eritrea","Eritrea","Eritrea","Eritrea","Eritrea","Eritrea")

group_var4.1 <- factor(group_var4.1, levels = c("Syria","Eritrea"))

dataset4.1 <- data.frame(term4.1,estimate4.1,group_var4.1)

plot4.1 <- ggplot(data=dataset4.1, aes(x=term4.1, y=estimate4.1, linetype = group_var4.1)) +
  geom_line(size = 0.8) +
  geom_point(shape = group_var2, size = 4) +
  theme(legend.title = element_blank(),
        legend.text=element_text(size=12),
        axis.text=element_text(size=12),
        legend.position = "bottom",
        plot.title = element_text(size = 20)) +
  scale_linetype_manual(values=c("solid", "twodash")) +
  scale_x_continuous(breaks=c(2015,2016,2017,2018,2019,2020,2021)) +
  xlab("") + ylab("") +
  labs(title = "(A)")


term4.2 <- rbind(2015,2016,2017,2018,2019,2020,
                 2015,2016,2017,2018,2019,2020,
                 2015,2016,2017,2018,2019,2020,
                 2015,2016,2017,2018,2019,2020)

estimate4.2 <- rbind(401,376,334,299,273,574,
                     650,617,508,529,623,842,
                     411,383,417,364,391,861,
                     711,597,528,567,484,969)

group_var4.2 <- rbind("Afghanistan","Afghanistan","Afghanistan","Afghanistan","Afghanistan","Afghanistan",
                      "Iran","Iran","Iran","Iran","Iran","Iran",
                      "Iraq","Iraq","Iraq","Iraq","Iraq","Iraq",
                      "Turkey","Turkey","Turkey","Turkey","Turkey","Turkey")

group_var4.2 <- factor(group_var4.2, levels = c("Afghanistan", "Iran", "Iraq", "Turkey"))

dataset4.2 <- data.frame(term4.2,estimate4.2,group_var4.2)

plot4.2 <- ggplot(data=dataset4.2, aes(x=term4.2, y=estimate4.2, linetype = group_var4.2)) +
  geom_line(size = 0.8) +
  geom_point(shape = group_var3, size = 4) +
  theme(legend.title = element_blank(),
        legend.text=element_text(size=12),
        axis.text=element_text(size=12),
        legend.position = "bottom",
        plot.title = element_text(size = 20)) +
  scale_linetype_manual(values=c("solid", "twodash", "dotted", "dotdash")) +
  scale_x_continuous(breaks=c(2015,2016,2017,2018,2019,2020,2021)) +
  xlab("") + ylab("") +
  labs(title = "(B)")

layout <- rbind(c(1,2))

final_figure4 <- grid.arrange(plot4.1, plot4.2, ncol=2, nrow=2, layout_matrix = layout)

ggsave(filename = "Figure_4_naturalization", height = 7, width = 13, plot = final_figure4, device = 'pdf', path = 'C:/Users/3349977/Desktop/Liber Amicorum Verkuyten')

